/*
 * BaseViewFrame.java
 * Conference Guru
 * version Autumn 2012
 * 
 * by Lawrence Grass
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Portal;

@SuppressWarnings("serial")
public class BaseViewFrame extends JFrame
						   implements ActionListener,
						   WindowListener, KeyListener
	{
	private final String FILE_NAME = "PORTAL.STATE";
	
	public static final Color BACKGROUND_COLOR = new Color(179, 193, 205); //new Color(200, 220, 255), "Slate"Color.LIGHT_GRAY;

	public static final int FRAME_WIDTH = 1200;
	
	public static final int FRAME_HEIGHT = 640;
	
	ConferenceViewPanel my_main_panel;
	
	JTextField my_login_field = new JTextField(25);
	
	String my_login_string;
	
	JButton my_login_button = new JButton("Login");
	
	JPanel base_login_panel = new JPanel();
	
	Portal my_portal;
	
	

	public BaseViewFrame()
	{
		super("Conference Guru");
		this.setResizable(false);		
		this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		this.addWindowListener(this);
		my_login_field.addKeyListener(this);
		my_portal = new Portal();
		my_portal = Portal.load(FILE_NAME);
		base_login_panel.setBackground(BaseViewFrame.BACKGROUND_COLOR);
		base_login_panel.setLayout(null);
		setContentPane(base_login_panel);
		
		welcomeMessage();
		setup_login();
				
		
	}
	
	/*
	 * Sets up the welcome message displayed above the login panel.
	 */
	private void welcomeMessage() {
		JPanel welcome = new JPanel();
		welcome.setOpaque(false);
		welcome.setLayout(new BoxLayout(welcome, BoxLayout.Y_AXIS));
		welcome.setBounds((this.getWidth() - this.getWidth()/2) - 225,
						  (this.getHeight() - this.getHeight()/2) - 100,
						  450, 50);
		JLabel welcome_text1 = new JLabel("              Welcome to the Conference Guru!");
		welcome_text1.setFont(new Font("verdana", Font.BOLD, 16));
		JLabel welcome_text2 = new JLabel("Please enter your user name before we continue.");
		welcome_text2.setFont(new Font("verdana", Font.BOLD, 16));

		welcome.add(welcome_text1);
		welcome.add(welcome_text2);
		base_login_panel.add(welcome);
	}
	
	public void logout()
	{
		setContentPane(base_login_panel);
		my_login_field.requestFocus();
		this.repaint();
	}
	
	public void login()
	{
		my_main_panel = new ConferenceViewPanel(my_portal, my_login_string, this);
		add(my_main_panel, BorderLayout.CENTER);
		setContentPane(my_main_panel);//hands the view off to the SubViewPanel
		my_main_panel.revalidate();
	}

	/*
	 * Sets up the login panel with the text field and login button.
	 */
	private void setup_login() {
		
		//The beveled login box
		JPanel login_panel = new JPanel();
		login_panel.setBounds((this.getWidth() - this.getWidth()/2) - 150,
							  (this.getHeight() - this.getHeight()/2) - 50,
							  300, 100);
		login_panel.setBackground(new Color(119,136,153));//(new Color(30,144,255));//Dodger Blue
		login_panel.setLayout(null);
		login_panel.setBorder(BorderFactory.createRaisedBevelBorder());
		
		//The Label and text field for the user name.
		JPanel user_panel = new JPanel(new BorderLayout());
		user_panel.setBounds((login_panel.getWidth() - login_panel.getWidth()/2) - 100,
							 (login_panel.getHeight() - login_panel.getHeight()/2) - 20,
							 200, 20);
		JLabel user_name_label = new JLabel("User Name: ");
		user_name_label.setOpaque(true);
		user_name_label.setBackground(new Color(119,136,153));
		user_name_label.setLabelFor(my_login_field);
		user_panel.add(my_login_field, BorderLayout.CENTER);
		user_panel.add(user_name_label, BorderLayout.WEST);
		login_panel.add(user_panel);
		
		my_login_button = new JButton("Login");
		my_login_button.addActionListener(this);
		my_login_button.setBounds((login_panel.getWidth() - login_panel.getWidth()/2) - 50,
							   (login_panel.getHeight() - login_panel.getHeight()/2) + 10,
							   100, 20);	
		login_panel.add(my_login_button);
		
		base_login_panel.add(login_panel);
		
		
		
	}
	
	/*
	 * Gets the user name from the user and hands display off to the next panel,
	 * which is the SubViewPanel.
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (!"".equals(my_login_field.getText()))
		{
			my_login_string = my_login_field.getText();
			my_login_field.setText("");
			my_main_panel = new ConferenceViewPanel(my_portal, my_login_string, this);
			this.remove(base_login_panel);//gets rid of the login screen
			
			setLayout(new BorderLayout());
			add(my_main_panel, BorderLayout.CENTER);
			setContentPane(my_main_panel);//hands the view off to the SubViewPanel
			my_main_panel.revalidate();
		}
		else 
		{
			JOptionPane.showMessageDialog(null, "We need a user name before we can continue",
					"Need a user name", JOptionPane.PLAIN_MESSAGE);
		}
			
	}

	@Override
	public void windowActivated(WindowEvent arg0) {}

	@Override
	public void windowClosed(WindowEvent arg0) {}

	/*
	 * Saves automatically when you exit the program.
	 */
	@Override
	public void windowClosing(WindowEvent arg0) {
		my_portal.save(FILE_NAME);
		this.dispose();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {}

	@Override
	public void windowDeiconified(WindowEvent arg0) {}

	@Override
	public void windowIconified(WindowEvent arg0) {}

	@Override
	public void windowOpened(WindowEvent arg0) {}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			my_login_button.doClick();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {}

	
	public void keyTyped(KeyEvent e) {}
}
